package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param roleDto
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<RoleVo>> page(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody RoleDto roleDto) {
        PageResponse<RoleVo> pageResponse =roleService.page(pageNum, pageSize, roleDto);
        return ResponseResult.success(pageResponse);
    }

    /**
     *根据角色查询选中的资源数据
     * @param roleId
     * @return
     */
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>>findResources(@PathVariable Long roleId){
        List<String> resources = roleService.findResources(roleId);
        return ResponseResult.success(resources);
    }

    /**
     * 添加角色
     * @param roleDto
     * @return
     */
    @PutMapping
    public ResponseResult add(@RequestBody RoleDto roleDto){
        roleService.add(roleDto);
        return ResponseResult.success(true);
    }

    /**bbb
     * 角色更新
     * @param roleDto
     * @return
     */
    @PatchMapping
    public ResponseResult update(@RequestBody RoleDto roleDto){
        roleService.update(roleDto);
        return ResponseResult.success(true);
    }

    /**
     * 角色删除
     * @param roleIds
     * @return
     */
    @DeleteMapping("/{roleIds}")
    public ResponseResult delete(@PathVariable Long roleIds ){
        roleService.delete(roleIds);
        return ResponseResult.success();
    }
    @PostMapping("/init-roles")
    public ResponseResult<List<RoleVo>> initRoles(){
       List<RoleVo> roles =roleService.initRoles();
        return ResponseResult.success(roles);
    }



}
